using System;
using System.Collections.Generic;
using System.IO;
using System.Web;

namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers.DNN6
{
    public class VentrianNewsArticlesThumbnailsUrlRewriter : IUrlRewriterProvider, IIsActive
    {

        //public bool IsActive
        //{
        //    get { return DotNetNuke.Framework.Reflection.CreateType("Ventrian.NewsArticles.CategoryController, Ventrian.NewsArticles", true) != null; }
        //}

        public bool IsActive
        {
            get { return true; }
        }

        List<GroupParamsRewrite> IFriendlyUrlGroupProvider.GetRewrites()
        {
            List<GroupParamsRewrite> toReturn = new List<GroupParamsRewrite>();
            string desc = "relative path to the folder containing the original image";
            toReturn.Add(new GroupParamsRewrite("imageFolderPath", desc, RewriteType.SubPath, UrlParam.FromSyntax("FileName"), UrlParam.FromSyntax("HomeDirectory")));
            toReturn[toReturn.Count - 1].UrlParams[UrlParam.FromSyntax("FileName")] = ParamNature.NonExclusive | ParamNature.Mandatory;
            desc = "name of the original image file, without extension";
            toReturn.Add(new GroupParamsRewrite("imageFileName", desc, RewriteType.PageName, UrlParam.FromSyntax("FileName")));
            toReturn[toReturn.Count - 1].UrlParams[UrlParam.FromSyntax("FileName")] = ParamNature.NonExclusive | ParamNature.Mandatory;
            desc = "extension of the original image file";
            toReturn.Add(new GroupParamsRewrite("imageExtension", desc, RewriteType.PageName, UrlParam.FromSyntax("FileName")));
            toReturn[toReturn.Count - 1].UrlParams[UrlParam.FromSyntax("FileName")] = ParamNature.NonExclusive | ParamNature.Mandatory;
            return toReturn;
        }


        GroupRewriteResult IUrlRewriterProvider.RewriteParams(string groupName, RewriteType objRewriteType, Dictionary<UrlParam, string> parameters, FriendlierUrlStrategy behaviour)
        {
            GroupRewriteResult toReturn = new GroupRewriteResult();

            if (parameters.Count > 0)
            {
                var fullRelativepath = GetFullRelativeFilePath(parameters);
                switch (groupName.ToLowerInvariant())
                {
                    case "imagefolderpath":
                        var fileName = Path.GetFileName(fullRelativepath);
                        toReturn.RewriteValue = fullRelativepath.Replace(fileName,"").Trim('/');
                        toReturn.ConsumedParameters[UrlParam.FromSyntax("FileName")] = true;
                        toReturn.ConsumedParameters[UrlParam.FromSyntax("HomeDirectory")] = true;
                        break;
                    case "imagefilename":
                        toReturn.RewriteValue = Path.GetFileNameWithoutExtension(fullRelativepath);
                        toReturn.ConsumedParameters[UrlParam.FromSyntax("FileName")] = true;
                        break;
                    case "imageextension":
                        toReturn.RewriteValue = Path.GetExtension(fullRelativepath).TrimStart('.');
                        toReturn.ConsumedParameters[UrlParam.FromSyntax("FileName")] = true;
                        break;
                }
            }
            return toReturn;
        }


        private string GetFullRelativeFilePath(Dictionary<UrlParam, string> parameters)
        {
            string relativeFilePath = HttpUtility.UrlDecode(parameters[UrlParam.FromSyntax("FileName")]);
            string relativeHomeDirectory;
            if (!parameters.TryGetValue(UrlParam.FromSyntax("HomeDirectory"), out relativeHomeDirectory))
            {
                relativeHomeDirectory = "";
            }
            return HttpUtility.UrlDecode( relativeHomeDirectory) + relativeFilePath;
        }



    }
}